package com.grape.service.base.impl;

import java.util.List;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.grape.dao.base.CourierRepository;
import com.grape.domain.base.Courier;
import com.grape.service.base.CourierService;

@Service
@Transactional
public class CourierServiceImpl implements CourierService{

	@Autowired
	private CourierRepository cr;
	
	@Override
	
	// @RequiresAuthentication 需要登录才能访问
	
	// 注解权限校验,我得到的角色权限是courier:add,再加冒号属于子权限,我的父权限是可以通吃的
	@RequiresPermissions("courier:add:10086")
	public void save(Courier courier) {
		cr.save(courier);
	}

	@Override
	public Page<Courier> findAll(Specification<Courier> spec,Pageable pageable) {
		return cr.findAll(spec,pageable);
	}

	@Override
	public void delBatch(String ids) {
		String[] split = ids.split(",");
		for (String str : split) {
			Integer id = Integer.parseInt(str);
			cr.updateDelTag(id);
		}
	}

	@Override
	public void restoreBatch(String ids) {
		String[] split = ids.split(",");
		for (String str : split) {
			Integer id = Integer.parseInt(str);
			cr.updateRestoreTag(id);
		}
	}

	@Override
	public List<Courier> findNoAssaciation() {
		// 条件查法
		/*Specification<Courier> sc = new Specification<Courier>() {
			
			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate p1 = cb.isEmpty(root.get("fixedAreas").as(Set.class));
				return p1;
			}
		};
		
		return cr.findAll(sc);*/
		
		// 直接给dao层一个查询语句算了
		return cr.findNoAssaciation();
	}

	@Override
	public List<Courier> fa() {
		return cr.findAll();
	}


	
}
